package com.yuanli.cosmeticmirror.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kongzue.dialog.v3.CustomDialog;
import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.BaseActivity;
import com.yuanli.cosmeticmirror.bean.FavoriteBean;
import com.yuanli.cosmeticmirror.model.iview.IFavoriteView;
import com.yuanli.cosmeticmirror.persenter.FavoritePersenter;
import com.yuanli.cosmeticmirror.view.adapter.FavoriteAdapter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的收藏
 */
public class FavoriteActivity extends BaseActivity implements IFavoriteView {
    @BindView(R.id.favorite_back)
    ImageView favoriteBack;
    @BindView(R.id.favorite_rv)
    RecyclerView favoriteRv;
    private FavoriteAdapter adapter;
    private FavoritePersenter persenter;
    private int i;

    @Override
    protected int getContentView() {
        return R.layout.activity_favorite;
    }

    @Override
    protected void init(Bundle bundle) {
        persenter = new FavoritePersenter(this, this);
    }


    @Override
    protected void initView() {
        adapter = new FavoriteAdapter();
        favoriteRv.setLayoutManager(new GridLayoutManager(this, 2));
        favoriteRv.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                FavoriteBean.DataBean dataBean = (FavoriteBean.DataBean) adapter.getItem(position);
                if (dataBean != null) {
                    startActivity(new Intent(FavoriteActivity.this, VideoPlayActivity.class).putExtra("isFavorite", true).putExtra("url", dataBean));
                }
            }
        });
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                i = position;
                FavoriteBean.DataBean item = (FavoriteBean.DataBean) adapter.getItem(position);
                if (view.getId() == R.id.favorite_more) {
                    if (item != null) {
                        getCustomDialog(item.getId());
                    }
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        showLoadDialog();
        persenter.getFavorite();
    }

    @Override
    protected void destroy() {

    }


    @OnClick(R.id.favorite_back)
    public void onViewClicked() {
        finish();
    }

    /**
     * 我的收藏界面  删除
     *
     * @param id
     * @return
     */
    private CustomDialog getCustomDialog(int id) {
        return CustomDialog.show(FavoriteActivity.this, R.layout.dialog_delete_favorite, (dialog, v) -> {
            TextView btnOk = v.findViewById(R.id.favorite_enter);
            TextView btncancel = v.findViewById(R.id.favorite_cancel);
            btnOk.setOnClickListener(v1 -> {
                persenter.delete(String.valueOf(id));
                dialog.doDismiss();
            });
            btncancel.setOnClickListener(v1 -> dialog.doDismiss());
        }).setAlign(CustomDialog.ALIGN.DEFAULT);
    }

    @Override
    public void onSucess(FavoriteBean object) {
        dialogDismiss();
        if (object.getData() != null) {
            adapter.setNewInstance(object.getData());

        }
    }

    @Override
    public void onError() {
        dialogDismiss();
    }

    @Override
    public void deteleSucess() {
        adapter.remove(i);
    }
}
